package com.bbjob.controller;

import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.bbjob.dto.Result;
import com.bbjob.model.ExamInfo;
import com.bbjob.query.QueryExamInfo;
import com.bbjob.service.IExamInfoService;

@Api(tags = "examinfoApi", description = "考试管理")
@RestController
@RequestMapping(path = "examinfo", produces = "application/json;charset=utf-8")
public class ExamInfoController {
	
	@Resource
	private IExamInfoService examInfoService;
	
	@ApiOperation(value="考试录入", notes="考试录入")
	@ApiImplicitParam(name="exam", value="考试录入", required=true, dataType="ExamInfo")
	@RequestMapping(value="entry", method=RequestMethod.POST)
	public Result entryExaminee(@RequestBody ExamInfo exam) {
		
		ExamInfo ret = examInfoService.entryInfo(exam);
		
		return new Result(ret);
	}

	@ApiOperation(value="试卷-考生导入", notes="试卷-考生导入")
	@RequestMapping(value="import", method=RequestMethod.POST)
	public Map<String, Object> importExaminee(HttpServletRequest request,  @RequestParam(value="file", required=false) MultipartFile multipartFile) {
		
		Map<String, Object> returnMap = examInfoService.importExaminee(multipartFile, request);
		
		return returnMap;
	}
	
	@ApiOperation(value="考试列表", notes="考试列表")
	@ApiImplicitParam(name="exam", value="考试信息", required=true, dataType="QueryExamInfo")
	@RequestMapping(value="list", method=RequestMethod.POST)
	public Result list(@RequestBody QueryExamInfo exam) {
		
		// List<SysOrganizationStu> data = examineeService.selectByParams(examinee);
		
		// PageInfo<SysOrganizationStu> pageInfo = ; //new PageInfo<SysOrganizationStu>(data);
		return examInfoService.selectByParams(exam);
	}
	
	@ApiOperation(value="考试信息", notes="考试信息")
	@ApiImplicitParam(name="id", value="考试信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="get/{id}", method=RequestMethod.GET)
	public Result get(@PathVariable("id")Integer id) {
		
		//ExamInfo ret = examInfoService.selectByPrimaryKey(id);
		
		return examInfoService.selectByPrimaryKey(id);
	}
	
	@ApiOperation(value="修改考试信息", notes="修改考试信息（不包含状态）")
	@ApiImplicitParams({
			@ApiImplicitParam(name="id", value="考试信息id", required=true, dataType="Integer", paramType="path"),
			@ApiImplicitParam(name="exam", value="考试信息", required=true, dataType="ExamInfo")	
	})
	@RequestMapping(value="edit/{id}", method=RequestMethod.POST)
	public Result edit(@PathVariable("id")Integer id, @RequestBody ExamInfo exam) {
		
		exam.setId(id);
		
		this.examInfoService.updateStudent(exam, id);
		
		return new Result();
	}
	
	@ApiOperation(value="删除考试信息", notes="删除考试信息")
	@ApiImplicitParam(name="id", value="考试信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="del/{id}", method=RequestMethod.GET)
	public Result del(@PathVariable("id")Integer id) {
		
		examInfoService.delById(id);
		
		return new Result();
	}
	
	@ApiOperation(value="修改考试状态", notes="修改考试信息（只修改状态）")
	@ApiImplicitParams({
			@ApiImplicitParam(name="id", value="考试信息id", required=true, dataType="Integer", paramType="path"),
			@ApiImplicitParam(name="exam", value="考试信息", required=true, dataType="ExamInfo")	
	})
	@RequestMapping(value="updateStatus/{id}", method=RequestMethod.POST)
	public Result updateStatus(@PathVariable("id")Integer id, @RequestBody ExamInfo exam) {
		
		exam.setId(id);
		this.examInfoService.updateStudent(exam, id);
		
		if (exam.getStatus() == 3) {
			// 发布考试，下载试题信息到分发中心
			examInfoService.downloadQueBankFromCenter(id);	
		}
		
		return new Result();
	}
	
	@ApiOperation(value="同步考试", notes="同步考试")
	@ApiImplicitParam(name="id", value="考试信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="updateExamInfo/{id}", method=RequestMethod.POST)
	public Result updatePaper(@PathVariable("id")Integer id) {
		
		// 发布考试，下载试题信息到分发中心
		examInfoService.downloadQueBankFromCenter(id);	
		
		return new Result();
	}
	
	@ApiOperation(value="同步成绩", notes="同步成绩")
	@ApiImplicitParam(name="id", value="考试信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="syncExamScores/{id}", method=RequestMethod.GET)
	public Result syncExamScores(@PathVariable("id")Integer id) {
		
		return examInfoService.syncExamScoresById(id);	
	}
	
	
	
	
	
}
